package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-10-29
 */
@Api(value = "医院设置信息接口")
@RestController
@RequestMapping("/admin/hospitalSet")
public class AdminHospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * @Description 查询所有
     * @return: com.atguigu.yygh.common.R.R
     */
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = HospitalSet.class) })
    @GetMapping("/findAll")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }

    /**
     * @Description 条件查询
     * @param pageNum:
     * @param pageSize:
     * @param hospitalSetQueryVo:
     * @return: com.atguigu.yygh.common.R.R
     */
    @PostMapping("/findPage/{pageNum}/{pageSize}")
    public R findPage(@PathVariable Long pageNum,@PathVariable Long pageSize,@RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()),"hosname",hospitalSetQueryVo.getHosname());
        queryWrapper.eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()),"hoscode",hospitalSetQueryVo.getHoscode());
        hospitalSetService.page(page,queryWrapper);
        return R.ok().data("total",page.getTotal()).data("list",page.getRecords());
    }

    /**
     * @Description 删除
     * @param id:
     * @return: com.atguigu.yygh.common.R.R
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@ApiParam(value = "医院设置id", name = "id",type = "Long",required = true,defaultValue = "1")  @PathVariable Long id){
        boolean delete = hospitalSetService.removeById(id);
        if (delete){
            return R.ok().code(20000);
        }else {
            return R.error().code(20001);
        }
    }

    /**
     * @Description 批量删除
     * @param ids:
     * @return: com.atguigu.yygh.common.R.R
     */
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    /**
     * @Description 添加
     * @param hospitalSet:
     * @return: com.atguigu.yygh.common.R.R
     */
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        String signKey = System.currentTimeMillis() + new Random().nextInt(1000) + "";
        hospitalSet.setSignKey(MD5.encrypt(signKey));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return R.ok().code(20000);
        }else {
            return R.error().code(20001);
        }
    }

    /**
     * @Description 查询
     * @param id:
     * @return: com.atguigu.yygh.common.R.R
     */
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item",hospitalSet);
    }

    /**
     * @Description 修改
     * @param hospitalSet:
     * @return: com.atguigu.yygh.common.R.R
     */
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if (update){
            return R.ok().code(20000);
        }else {
            return R.error().code(20001);
        }
    }

    /**
     * @Description 修改状态
     * @param id:
     * @param status:
     * @return: com.atguigu.yygh.common.R.R
     */
    @PutMapping("/updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}

